use common_base::serde::{self, Serialize};

use super::{metadata::MetadataBase, Metadata};

#[derive(Debug, Serialize, Clone)]
#[serde(crate = "self::serde")]
#[serde(rename_all = "camelCase")]
pub struct FctMetadata {
    pub(crate) base: MetadataBase,
    pub(crate) fkey_dct_metadatas: Vec<String>,
}

impl Into<Metadata> for FctMetadata {
    fn into(self) -> Metadata {
        Metadata::Fct(self)
    }
}

impl FctMetadata {
  
    pub const SYS_FACTS: &'static str = "SYS_FACTS";
    pub const MAIN_DO_META_DATA: &'static str = "mainDoMetaData";

    pub fn new(fct_id: &str, rowset: RowsetOwner) -> Self {
        Self {
            base: MetadataBase::new(fct_id, rowset),
            fkey_dct_metadatas: Default::default(),
        }
    }
}

impl FctMetadata {
    pub fn get_object_id(&self) -> &str {
        &self.base.obj_id
    }

    pub fn set_do_metadata(&mut self, do_metadata: Metadata) {
        self.base
            .self_rowset
            .put_object(Self::MAIN_DO_META_DATA, do_metadata.into());
    }

    pub fn get_do_metadta(&self) -> Option<&Metadata> {
        self.base
            .self_rowset
            .get_object(Self::MAIN_DO_META_DATA)
            .map(|x| {
                x.as_object()
                    .unwrap()
                    .as_any()
                    .downcast_ref::<Metadata>()
                    .unwrap()
            })
    }

    pub fn set_fkey_dct_metadatas(&mut self, fkey_dct_metadatas: Vec<String>) {
        self.fkey_dct_metadatas = fkey_dct_metadatas;
    }
}